package com.example.xianji.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

public class HuanyingActivity extends Activity {
    private ViewPager viewpager;
    private int image[] = new int[]{R.drawable.huanying1, R.drawable.huanying2, R.drawable.huanying3, R.drawable.huanying4};
    private ArrayList<ImageView> list = new ArrayList<ImageView>();
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_huanying);

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        for (int i = 0; i < image.length; i++) {
            ImageView imageview = new ImageView(this);
            imageview.setImageResource(image[i]);
            imageview.setScaleType(ScaleType.CENTER_CROP);
            list.add(imageview);
        }
        handler = new Handler();
        viewpager.setAdapter(new ViewPagerAdapter());
        viewpager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == 3) {
                    PreferenceEngine.getInstance().putBoolean(Constant.SP.NAME,
                            Constant.SP.KEY_XIANJI_QIDONG_FLAG,true);
//                    ed.putBoolean("xianji_QiDong_flag", true);
//                    ed.commit();
                    handler.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            startActivity(new Intent(HuanyingActivity.this, MainActivity.class));
                            HuanyingActivity.this.finish();
                        }
                    }, 1000);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}